package org.sundatagroup.aurora.show.config;

import java.time.Instant;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.sundatagroup.aurora.show.service.ScreenStatusService;


/**
 * 定时清理不活动的屏幕的定时任务配置
 * @author lui
 * @date 2018-05-11
 * @since 0.2.0
 */
@Component
@EnableScheduling
public class ScreenStatusRemover {
	@Autowired
	ScreenStatusService statusQuerier;

	private final static Logger LOGGER = LoggerFactory.getLogger(ScreenStatusRemover.class);

	/**
	 * 每 60 秒扫描一次状态列表,
	 * 断开连接超过60秒的屏幕会被清除
	 * @throws Exception
	 */
	@Scheduled(fixedRate = 60000)
	public void screenTimeoutCheck() throws Exception {
		List<String> removeList = statusQuerier.timerFilt(60000, Instant.now());
		for (int i = 0; i < removeList.size(); i++) {
			statusQuerier.destroy(removeList.get(i));
		}
		
		LOGGER.info("剩余追踪展示屏的数量: " + statusQuerier.count());
	}

}
